package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.dao.PaasFormPaasSubDataDao;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.model.PaasFormSubDataEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.saas.constant.SubFormTableNameConstant;
import com.xbongbong.saas.domain.dao.PaymentTaskSubDao;
import com.xbongbong.saas.domain.entity.PaymentTaskSubDataEntity;
import com.xbongbong.saas.model.PaymentTaskSubDataModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: xingxing.xiao
 * @date: 2020/12/01 14:21
 */
@Service("paymentTaskSubDataModel")
public class PaymentTaskSubDataModelImpl implements PaymentTaskSubDataModel {
    @Resource
    private PaasFormPaasSubDataDao paasFormPaasSubDataDao;
    @Resource
    private PaasFormSubDataEsModel paasFormSubDataEsModel;
    @Resource
    private PaymentTaskSubDao paymentTaskSubDao;
    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public Integer insert(PaymentTaskSubDataEntity entity) throws XbbException {
        int insert = paasFormPaasSubDataDao.insert(entity, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.insert(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
        return insert;
    }

    @Override
    public Integer update(PaymentTaskSubDataEntity entity) throws XbbException {
        int update = paasFormPaasSubDataDao.update(entity, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.update(entity, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
        return update;
    }

    @Override
    public List<PaymentTaskSubDataEntity> findEntitys(Map<String, Object> param) {
        return (List<PaymentTaskSubDataEntity>) paasFormPaasSubDataDao.findEntitys(param, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return paasFormPaasSubDataDao.getEntitysCount(param, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer insertBatch(List<PaymentTaskSubDataEntity> entities) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.insertBatch(entities, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.insertBatch(entities, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
        return integer;
    }

    @Override
    public List<PaymentTaskSubDataEntity> getByDataId(Long dataId, String corpid) {
        return (List<PaymentTaskSubDataEntity>) paasFormPaasSubDataDao.getByDataId(dataId, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
    }

    @Override
    public List<PaymentTaskSubDataEntity> getByDataIds(List<Long> dataIds, String corpid, DelEnum delEnum) {
        return (List<PaymentTaskSubDataEntity>) paasFormPaasSubDataDao.getByDataIds(dataIds, corpid, delEnum.getDel(), SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateDataList, String corpid) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.updateBatch(updateDataList, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        if (!Objects.equals(0, integer)) {
            paasFormPaasSubDataDao.updateBatchTime(updateDataList, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
            paasFormSubDataEsModel.updateBatch(updateDataList, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, WriteRequest.RefreshPolicy.NONE);
        }
        return integer;
    }

    @Override
    public Integer deleteBatch(String corpid, List<PaymentTaskSubDataEntity> entities, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        List<Long> ids = new ArrayList<>();
        for (PaasFormSubDataEntity item : entities) {
            ids.add(item.getId());
        }
        Integer result = paasFormPaasSubDataDao.deleteById(ids, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, refreshPolicy, DelEnum.DELETE);
        return result;
    }

    @Override
    public Integer deleteByDataId(Long dataId, String corpid) throws XbbException {
        List<PaymentTaskSubDataEntity> entities = getByDataId(dataId, corpid);
        Integer result = paasFormPaasSubDataDao.deleteByDataId(dataId, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
        return result;
    }

    @Override
    public Integer deleteBatchByDataId(List<Long> dataIds, String corpid, DelEnum delEnum) throws XbbException {
        List<PaymentTaskSubDataEntity> entities = getByDataIds(dataIds, corpid, DelEnum.NORMAL);
        Integer integer = paasFormPaasSubDataDao.deleteBatchByDataId(dataIds, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA, delEnum.getDel());
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
        return integer;
    }

    @Override
    public void restoreBatch(List<Long> dataIds, String corpid) throws XbbException {
        List<PaymentTaskSubDataEntity> entities = getByDataIds(dataIds, corpid, DelEnum.DELETE_SUBFORM);
        paasFormPaasSubDataDao.restoreBatch(dataIds, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
    }

    /**
     * 根据id删除子表单数据
     *
     * @param paymentTaskSubDataIdMap
     * @param corpid 公司id
     * @return
     */
    @Override
    public Integer deleteBatchByIdIn(Map<Long, Long> paymentTaskSubDataIdMap, String corpid) throws XbbException{
        List<Long> idIn = new ArrayList<>();
        paymentTaskSubDataIdMap.forEach((key,value)->idIn.add(key));
        Integer integer = paasFormPaasSubDataDao.deleteById(idIn, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.deleteBatchByIdIn(corpid, paymentTaskSubDataIdMap, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE);
        return integer;
    }

    /**
     * 更新回款计划的子表单数据
     *
     * @param updatePaymentTaskSubDataList
     * @param corpid 公司id
     * @return
     */
    @Override
    public Integer updateBatchByEntity(List<PaymentTaskSubDataEntity> updatePaymentTaskSubDataList, String corpid) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.updateBatchByEntity(updatePaymentTaskSubDataList, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA);
        paasFormSubDataEsModel.updateBatchByEntity(updatePaymentTaskSubDataList, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, WriteRequest.RefreshPolicy.NONE);
        return integer;
    }

    /**
     * 无差别更新回款计划子表单数据
     *
     * @param paymentTaskSubDataEntities
     */
    @Override
    public void updateAllBatch(List<PaymentTaskSubDataEntity> paymentTaskSubDataEntities) throws XbbException {
        List<InsertDTO> insertDTOList = new ArrayList<>();
        Long updateTime = (long)DateUtil.getInt();
        for (PaymentTaskSubDataEntity entityExt : paymentTaskSubDataEntities){
            entityExt.setUpdateTime(updateTime);
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(ElasticConstant.SUB_FORM_TYPE+"_"+entityExt.getCorpid()+"_"+entityExt.getId());
            insertDTO.setParent(entityExt.getCorpid() + "_" + entityExt.getDataId());
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entityExt, paasFormDataEntity);
            paasFormDataEntity.setDataId(entityExt.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(paasFormDataEntity, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
            insertDTOList.add(insertDTO);
        }
        paymentTaskSubDao.updateBatchAll(paymentTaskSubDataEntities);
        paasEsModel.updateBatch(insertDTOList);
    }

    /**
     * 无差别插入回款计划子表单信息
     *
     * @param paymentTaskSubDataInsertList
     */
    @Override
    public void insertAllBatch(List<PaymentTaskSubDataEntity> paymentTaskSubDataInsertList) throws XbbException{
        paymentTaskSubDao.insertAllBatch(paymentTaskSubDataInsertList);
        paasFormSubDataEsModel.insertBatch(paymentTaskSubDataInsertList, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB);
    }

    @Override
    public Integer updateDel(List<PaymentTaskSubDataEntity> updateDelList, String corpid, WriteRequest.RefreshPolicy refreshPolicy, DelEnum delEnum) throws XbbException{
        List<Long> ids = new ArrayList<>();
        for (PaasFormSubDataEntity v : updateDelList) {
            ids.add(v.getId());
        }
        Integer result = paasFormPaasSubDataDao.updateDelByIdList(ids, corpid, SubFormTableNameConstant.TB_SAAS_PAYMENT_TASK_SUB_DATA, delEnum.getDel());
        paasFormSubDataEsModel.deleteBatch(corpid, updateDelList, IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_SUB, refreshPolicy, delEnum);
        return result;
    }
}
